package cn.kgc.crowdfundingadmin02.service;

import cn.kgc.crowdfundingadmin02.mapper.RoleMapper;
import cn.kgc.crowdfundingadmin02.pojo.Role;
import cn.kgc.crowdfundingadmin02.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-26 9:31
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {


    //注入
    @Resource
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
      //1.分页
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%"+keyword+"%");  //根据名称进行模糊查询
        List<Role> roles = roleMapper.selectByExample(example);
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return pageInfo;
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        //创建 实体类Role 对应的example对象
        RoleExample example=new RoleExample();
        //在example中 封装对象 查询条件
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);

        //执行查询
        return roleMapper.selectByExample(example);
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);
        roleMapper.deleteByExample(example);
    }

    @Override
    public void saveRole(String roleName) {
       /* RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(roleName);
        roleMapper.insert(null,roleName);*/
        roleMapper.insert(new Role(null,roleName));
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return  roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {

        //删除旧的
        roleMapper.deletAdminRelationRole(adminId);
        //添加新的

        if(roleIdList!=null){
            roleMapper.insertAdminRelationRole(adminId,roleIdList);
        }

    }
}
